package com.situ.mall.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.mall.listener.ProductExcelListener;
import com.situ.mall.pojo.Product;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.pojo.vo.ProductExcelVO;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.util.ExcelUtil;
import com.situ.mall.util.PageResult;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2025-06-17
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult<List<ProductVO>> list(ProductQuery productQuery) {
        // select * from product where name='zhang' and create_time between xxx and xxx limit 0,10;
        IPage<ProductVO> page = new Page<>(productQuery.getPage(), productQuery.getLimit());
        productMapper.list(page, productQuery);

        return new PageResult(0, "", (int)page.getTotal(), page.getRecords());
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<Product> list = productMapper.selectList(null);
        List<ProductExcelVO> productExcelVOList = new ArrayList<>();
        for (Product product : list) {
            ProductExcelVO productExcelVO = new ProductExcelVO();
            BeanUtils.copyProperties(product, productExcelVO);
            productExcelVOList.add(productExcelVO);
        }
        ExcelUtil.exportExcel(response, productExcelVOList, ProductExcelVO.class, "product");

    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ProductExcelVO.class, new ProductExcelListener(productMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //@Override
    public PageResult list1(ProductQuery productQuery) {
        // select * from product where name='zhang' and create_time between xxx and xxx limit 0,10;
        IPage<Product> page = new Page<>(productQuery.getPage(), productQuery.getLimit());
        //查询条件
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(productQuery.getName()), "name", productQuery.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(productQuery.getSubtitle()), "subtitle", productQuery.getSubtitle());
        queryWrapper.between(!ObjectUtils.isEmpty(productQuery.getBeginCreateTime()) && !ObjectUtils.isEmpty(productQuery.getEndCreateTime()),
                "create_time", productQuery.getBeginCreateTime(), productQuery.getEndCreateTime() );
        productMapper.selectPage(page, queryWrapper);

        return new PageResult(0, "", (int)page.getTotal(), page.getRecords());
    }
}
